<template>
	<view v-if="goodsDetailData.goods_name" class="goods-detail-container">

		<!-- 轮播区域 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
			<swiper-item v-for="(item,index) in goodsDetailData.pics" :key="index">
				<image :src="item.pics_big" @click="preview(index)"></image>
			</swiper-item>
		</swiper>

		<!-- 商品信息区域 -->
		<view class="goods-info-box">
			<!-- 商品价格 -->
			<view class="price">￥{{goodsDetailData.goods_price}}</view>
			<!-- 商品信息主体 -->
			<view class="goods-info-body">
				<!-- 商品名字 -->
				<view class="goods-name">{{goodsDetailData.goods_name}}</view>
				<!-- 商品搜藏 -->
				<view class="favi">
					<uni-icons type="star" size="18" color="gray"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<!-- 运费 -->
			<view class="yf">快递：免运费</view>
		</view>

		<!-- 商品详情图 -->
		<rich-text :nodes="goodsDetailData.goods_introduce"></rich-text>

		<!-- 底部导航按钮 -->
		<view class="goods_nav">
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="leftNavClick"
				@buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	// 导入vuex中的mapState
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'

	export default {
		computed: {
			// 使用mapState方法使引用更简单
			...mapState('my_cart', []),
			// 使用mapGetters方法使引用更简单
			...mapGetters('my_cart', ['total'])
		},
		// 监听购物车总数数据变化改变购物车角标
		watch: {
			// ---BUG--------------BUG---------------BUG-------------BUG-----------初始页面加载后未能获取到total
			// total(value) {
			// 	const findResult = this.options.find(x => x.text === '购物车')
			// 	if (findResult) {
			// 		findResult.info = value
			// 	}
			// }
			// ---BUG--------------BUG---------------BUG-------------BUG-----------
			// 深度监听加载页面即调用
			total:{
				handler(value){
					const findResult = this.options.find(x => x.text === '购物车')
					if (findResult) {
						findResult.info = value
					}
				},
				immediate:true
			}
		},
		data() {
			return {
				// 商品详情数据
				goodsDetailData: [],
				// 底部导航数据
				options: [{
					icon: 'shop',
					text: '店铺',
					infoBackgroundColor: '#007aff',
					infoColor: "red"
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			};
		},
		methods: {
			// 获取商品详情数据
			async getGoodsDetail(goods_id) {
				const {
					data
				} = await uni.$http.get('/api/public/v1/goods/detail', {
					goods_id: goods_id
				})
				// 若数据获取异常弹出消息
				if (data.meta.status !== 200) {
					return uni.$showMsg()
				}
				// 将数据中的goods_introduce的html正则替换成具有样式的image
				data.message.goods_introduce = data.message.goods_introduce.replace(/<img /g,
					'<img style="display:block;" ').replace(/webp/g, 'jpg')
				// 赋值
				this.goodsDetailData = data.message
			},
			// 轮播图片预览
			preview(index) {
				uni.previewImage({
					current: index,
					urls: this.goodsDetailData.pics.map(item => item.pics_big),
					loop: true
				})
			},
			// 底部导航左侧点击
			leftNavClick(e) {
				if (e.index === 1) {
					uni.switchTab({
						url: '/pages/cart/cart'
					})
				}
			},
			// 加入购物车按钮
			buttonClick(e) {
				if (e.index === 0) {
					const goods = {
						goods_id: this.goodsDetailData.goods_id,
						goods_name: this.goodsDetailData.goods_name,
						goods_price: this.goodsDetailData.goods_price,
						goods_count: 1,
						goods_small_logo: this.goodsDetailData.goods_small_logo,
						goods_state: true
					}
					// 使用mutations方法修改store数据
					this.addToCart(goods)
				}
			},
			// 使用mapMutations方法使引用更简单
			...mapMutations('my_cart', ['addToCart'])
		},
		onLoad(options) {
			// 加载来源数据
			const goods_id = options.goods_id
			this.getGoodsDetail(goods_id)
		}
	}
</script>

<style lang="scss">
	.goods-detail-container {
		padding-bottom: 50px;
	}

	swiper {
		height: 700rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.goods-info-box {
		padding: 10px;
		padding-right: 0;

		.price {
			color: #c00000;
			font-size: 18px;
			margin: 10px 0;
		}

		.goods-info-body {
			display: flex;
			justify-content: space-between;

			.goods-name {
				font-style: 13px;
				margin-right: 10px;
			}

			.favi {
				width: 120px;
				font-style: 12px;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				border-left: 1px solid #efefef;
				color: gray;
			}
		}

		.yf {
			font-size: 12px;
			color: gray;
			margin: 10px 0;
		}
	}

	.goods_nav {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}
</style>